<?php
class MessageController extends Ctrl_Base{
    public function indexAction(){
		$this->sqlite()->query('update message set status = 1 where status  = 0');
		$tLimit = 10;                                  
        $tContArr = $this->sqlite()->getRow('select count(*) c from message where language = \''.$this->l.'\'');                
        $tCnt = empty($tContArr['c'])?0:$tContArr['c'];                                               
        $tPage = new Tool_Page($tCnt, $tLimit);                                                        
		$tDatas = array();
		if(!empty($tCnt)){
        	$tDatas =$this->sqlite()->getAll('select * from message where language = \''.$this->l.'\' order by created desc limit ' . $tPage->limit());        
		}
        $this->assign('tDatas' , $tDatas);                                                          
        $this->assign('pageinfo', $tPage->show());
        $this->assign('tHeadact', 'message');
		$this->display('Message/index');
    }

	public function checknewmessageAction(){
		$tMywallet = $this->sqlite()->getAll('select * from mywallet where status=3');

		$tMList = new ListtransactionsModel;
		foreach($tMywallet as $tRow){

			$tCount = $this->sqlite()->getRow('select count(0) c from message where wallet = \'' .$tRow['mark']. '\'');	
			$tC = 20;
			if(empty($tCount['c'])){
				$tC = 100000;	
			}
			#$tTransactions = Tool_Fnc::rpc_client($tRow)->listtransactions('*',$tC,0);
			$tTransactions = Tool_Fnc::rpc_client($tRow)->listtransactions();
			if(!count($tTransactions)) continue;
			$tTransactions = $tMList->listtransactions_exclude($tTransactions);
			foreach($tTransactions as $tTRow){
				if($tRow['mark'] != $tTRow['symbol']){continue;}
				if($tTRow['category'] != 'receive'){continue;}
				$tMRow = $this->sqlite()->getRow('select * from message where txid = \'' .$tTRow['txid']. '\' and wallet = \'' .$tRow['mark']. '\'');	
				if(count($tMRow) && is_array($tMRow)) continue;
				$tData = array(
					'content' => sprintf('<a class="modify" href="/wallet/listtransactions?wallet='.$tRow['mark'].'">您有一笔%s到账，数量为:&nbsp;',$tRow['name']) . sprintf('%6f',$tTRow['amount']) . '</a>',		
					'created' => $tTRow['time'],
					'language' => 'cn',
					'status' => 0,
					'txid' => $tTRow['txid'],
					'wallet' => $tRow['mark'],
				);	
				$this->sqlite()->insert('message' , $tData);
				$tData = array(
					'content' => sprintf('<a class="modify" href="/wallet/listtransactions?wallet='.$tRow['mark'].'">You have received a new %s transfer, amount:&nbsp;',$tRow['en_name']) . sprintf('%6f',$tTRow['amount']) . '</a>',		
					'created' => $tTRow['time'],
					'language' => 'en',
					'status' => 0,
					'txid' => $tTRow['txid'],
					'wallet' => $tRow['mark'],
				);	
				$this->sqlite()->insert('message' , $tData);

			}
		}

		$tCount = $this->sqlite()->getRow('select count(*) c from message where status = 0 and language = \''.$this->l.'\'');
		$tCount = empty($tCount['c'])?0:$tCount['c'];
		Tool_Fnc::ajaxMsg( '', 1 , array('messagecount' => $tCount) );
	}
}
